package com.css.fxfzaqbz.modules.assess1.repository.impl;

import com.css.fxfzaqbz.modules.assess1.entity.DestoryRateEntity;
import com.css.fxfzaqbz.modules.assess1.queryParams.DestoryRateQueryParams;
import com.css.fxfzaqbz.modules.assess1.repository.DestoryRateRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import com.css.fxfzaqbz.zcpt.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-11-28 16:22
 */
@Repository
public class DestoryRateRepositoryImpl implements DestoryRateRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SUserService sUserService;

    @Override
    public List<DestoryRateEntity> getPage(DestoryRateQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from sys_destory_rate");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        StringBuilder orderSql = new StringBuilder(" order by prefecture,structure_type,intensity ");



        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())){
            whereSql.append(" and province = '"+queryParams.getProvince()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())){
            whereSql.append(" and city = '"+queryParams.getCity()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getPrefecture())){
            whereSql.append(" and prefecture = '"+queryParams.getPrefecture()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getStructureType())){
            whereSql.append(" and structure_type = '"+queryParams.getStructureType()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getIntensity())){
            whereSql.append(" and intensity = '"+queryParams.getIntensity()+"'");
        }
        sql.append(whereSql).append(orderSql);

        List<DestoryRateEntity> list = null;

        list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DestoryRateEntity.class));

        return list;
    }

    @Override
    public void update(DestoryRateEntity destoryRateEntity) {
        String sql="update sys_destory_rate set zero = :zero, one= :one ,two = :two ,three = :three " +
                ",four = :four ,update_user = :updateUser ,update_time = :updateTime where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(destoryRateEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<DestoryRateEntity> isHaveRepeatData(DestoryRateEntity destoryRateEntity) {
        StringBuilder sql = new StringBuilder("select * from sys_destory_rate");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");



        if(!PlatformObjectUtils.isEmpty(destoryRateEntity.getProvince())){
            whereSql.append(" and province = '"+destoryRateEntity.getProvince()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(destoryRateEntity.getCity())){
            whereSql.append(" and city = '"+destoryRateEntity.getCity()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(destoryRateEntity.getPrefecture())){
            whereSql.append(" and prefecture = '"+destoryRateEntity.getPrefecture()+"'");
        }
        if(!PlatformObjectUtils.isEmpty(destoryRateEntity.getStructureType())){
            whereSql.append(" and structure_type = '"+destoryRateEntity.getStructureType()+"'");
        }
/*        if(!PlatformObjectUtils.isEmpty(destoryRateEntity.getIntensity())){
            whereSql.append(" and intensity = '"+destoryRateEntity.getIntensity()+"'");
        }*/

        sql.append(whereSql);

        List<DestoryRateEntity> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(DestoryRateEntity.class));

        return list;
    }

    @Override
    public void save(DestoryRateEntity destoryRateEntity) {
        String sql="insert into sys_destory_rate (id,province,city,prefecture,structure_type,zero,one,two,three,four,create_user,create_time) values(:id, :province, :city, :prefecture, :structureType, :zero, :one, " +
                ":two, :three, :four, :createUser, :createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(destoryRateEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delete(DestoryRateEntity destoryRateEntity) {
        String sql="delete from sys_destory_rate where province='"+destoryRateEntity.getProvince()+"' and city='"+destoryRateEntity.getCity()+"' and prefecture='"+destoryRateEntity.getPrefecture()+"' ";
        SqlParameterSource source = new BeanPropertySqlParameterSource(destoryRateEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

}
